import { oDialog } from 'oneui/index'
import { onMounted, onUnmounted } from 'vue'

export const AddUserDialog = async () => {
  const res2 = await useFetch('/api/hello')
  console.log(res2)
  const app = oDialog.open(() => {
    const checkAge = (rule: any, value: any, callback: any) => {
      if (!value) {
        return callback(new Error('请填写真实自定义名称'))
      }
      callback()
    }
    const dialogOptions = {
      title: '添加',
      width: 400,
      columns: [
        {
          prop: 'agent',
          placeholder: '请输入直属分组',
          comtype: 'input',
          label: '直属分组',
          require: true,
          rules: [{ validator: checkAge, trigger: 'blur' }] // 自定义验证规则
        },
        {
          prop: 'name',
          placeholder: '请输入账号',
          comtype: 'input',
          require: true,
          label: '用户账号'
        }
      ]
    }

    const onSubmit = async (formData: any) => {
      console.log('onSubmit', formData)
      await new Promise(r => setTimeout(r, 2000))
    }

    onMounted(() => {
      console.log('mounted生命周期')
    })
    onUnmounted(() => {
      console.log('unmounted生命周期')
    })

    return {
      dialogOptions,
      onSubmit // 【回调】校验成功的回调
      // onValidError, // 【回调】校验失败的回调
      // onCancel  // 【回调】关闭弹出触发的回调
      // 【可主动验证】传字段数组 app.validate(['age']) 校验某些字段，结果会走onSubmit、onValidError
      // 【可清理验证】传字段数组 app.clearValidate(['age']) 可传某些字段或者不传就是全部
    }
  })
}
